package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.UListElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.ApplicationItem;
import com.mbc.web.common.client.ClientSingleton;

public class SidebarTag extends Composite {

	@UiField UListElement g_side_menu;
	@UiField HTMLPanel sidebar;
	
	private static SidebarTagUiBinder uiBinder = GWT
			.create(SidebarTagUiBinder.class);

	interface SidebarTagUiBinder extends UiBinder<Widget, SidebarTag> {
	}

	public SidebarTag() {
		initWidget(uiBinder.createAndBindUi(this));
		sidebar.getElement().setId("sidebar");
		g_side_menu.setId("side-menu");
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				generateMenu(ClientSingleton.appItemList);
				makeMenuScroll();
				showFirstListGroup();
			}
		});
	}
	
	private native void showFirstListGroup()/*-{
		$wnd.$("#side-menu > li:first-child > ul").collapse("show");
	}-*/;
	
	private native void setListItemTransaction()/*-{
		$wnd.$("#side-menu>li>ul").collapse({toggle: false});
		$wnd.$("#side-menu a").click(
						function() {
							if ($wnd.$(this).parent().parent().hasClass("collapse")) {
								if ($wnd.$(".breadcrumb li").length === 2) {
									var l_parent = $wnd.$(this).parent().parent().prev()
											.text();
									$wnd.$("<a/>").text(
											l_parent).appendTo(
											$wnd.$("<li/>").insertBefore(
													".breadcrumb .active"));
								}
							} else {
								if ($wnd.$(".breadcrumb li").length === 3)
									$wnd.$(".breadcrumb li:nth-child(2)")
											.remove();
							}
							$wnd.$(".breadcrumb .active").text(
									$wnd.$(this).text());
						});
	
		$wnd.$("#side-menu>li>a").click(
				function() {
					$wnd.$("#side-menu>li>ul").collapse("hide");
					$wnd.$(this).siblings().collapse("show");
	
					if ($wnd.$(this).parent().hasClass("active")) {
						$wnd.$(this).parent().removeClass("active");
					} else {
						$wnd.$("#side-menu li.active").removeClass("active");
						$wnd.$(this).parent().addClass("active");
					}
				});
	}-*/;
	
	public void generateMenu(List<ApplicationItem> appItems) {
		if(appItems != null)
		{
			if(appItems.size() > 0)
			{
				for (ApplicationItem l_item : appItems) {
					if (l_item.getParentId() == 0) {
						Element l_li = DOM.createElement("li");
						
						Element l_ancher = DOM.createAnchor();
						l_ancher.setAttribute("data-code", l_item.getCode());
						l_ancher.setInnerText(l_item.getName());
						setChangeViewEvent(l_ancher);
						l_li.appendChild(l_ancher);
						g_side_menu.appendChild(l_li);

						Element l_ul = DOM.createElement("ul");
						l_ul.setClassName("nav nav-second-level collapse");

						for (ApplicationItem l_child : appItems) {
							if (l_child.getParentId() == l_item.getAppItemId()) {
								Element l_sub_li = DOM.createElement("li");
								Element l_listItem = DOM.createAnchor();
								l_listItem.setAttribute("data-code", l_child.getCode());
								l_listItem.setInnerText(l_child.getName());
								setChangeViewEvent(l_listItem);
								l_sub_li.appendChild(l_listItem);
								l_ul.appendChild(l_sub_li);
							}
						}
						if (l_ul.getChildCount() > 0) {
							l_li.appendChild(l_ul);
						}
					}
				}
				setListItemTransaction();
			}
		}
	}
	
	private void setChangeViewEvent(Element l_ancher)
	{
		Event.sinkEvents(l_ancher, Event.ONCLICK);
		Event.setEventListener(l_ancher, new EventListener() {
			
			@Override
			public void onBrowserEvent(Event event) {
				com.google.gwt.dom.client.Element l_item = Element.as(event.getCurrentEventTarget());
				String l_current_code = l_item.getAttribute("data-code");
				ContentTag.isViewLoaded = false;
				if(!l_current_code.equals("") && ClientSingleton.menuClickHandler != null)
				{
					ClientSingleton.setCurrentAppItem(l_current_code);
					ClientSingleton.menuClickHandler.onClick(null);
					if(ContentTag.isViewLoaded)
						hideSidebar();
				}
			}
		});
	}
	
	private native void hideSidebar()/*-{
		if($wnd.$("body").css("padding-left") === "0px"){
			$wnd.$("#sidebar").attr("class","sidebar");
		}
	}-*/;
	
	private native void makeMenuScroll()/*-{
		var scrollHeight = $wnd.windowHeight - $wnd.$("#menubar").height();
    	$wnd.$('#navigatorDiv').slimScroll({
	        height: scrollHeight,
	        position: 'right'
    	});
    	
    	$wnd.$(window).resize(function () {
	        var scrollHeight = $wnd.windowHeight - $wnd.$("#menubar").height();
	        $wnd.$("#navigatorDiv").css("height", scrollHeight + "px").parent().css("height", scrollHeight + "px");
	    });
	}-*/;
}
